$(function () {
    $.getJSON("/recentSale", function (result) {
        console.log(result);
        var arr = [];
        var arr2 = [];
        var arr3 = [];
        var a2 = result["total"];
        $("#totalSale").text("近一周总销量:" + a2);
        for (i = 0; i < result["top7"].length; i++) {
            var obj = result["top7"][i];
            arr.push(obj["Like"]);
            arr2.push(((arr[i] / parseFloat(a2)) * 100).toPrecision(2));
            arr3.push(obj["Name"].toString() + " " + ((arr[i] / parseFloat(a2)) * 100).toPrecision(2).toString() + "%")
        }
        var ctx, data, myLineChart, options;
        Chart.defaults.global.responsive = true;
        ctx = $('#jumbotron-line-chart').get(0).getContext('2d');
        options = {
            showScale: false,
            scaleShowGridLines: true,
            scaleGridLineColor: "rgba(0,0,0,.05)",
            scaleGridLineWidth: 1,
            scaleShowHorizontalLines: true,
            scaleShowVerticalLines: true,
            bezierCurve: false,
            bezierCurveTension: 0.4,
            pointDot: true,
            pointDotRadius: 4,
            pointDotStrokeWidth: 1,
            pointHitDetectionRadius: 20,
            datasetStroke: true,
            datasetStrokeWidth: 2,
            datasetFill: true,
            legendTemplate: "<ul class=\"<%=name.toLowerCase()%>-legend\"><% for (var i=0; i<datasets.length; i++){%><li><span style=\"background-color:<%=datasets[i].strokeColor%>\"></span><%if(datasets[i].label){%><%=datasets[i].label%><%}%></li><%}%></ul>"
        };
        data = {
            labels: result["week"],
            datasets: [
                {
                    label: "My Second dataset",
                    fillColor: "rgba(34, 167, 240,0.2)",
                    strokeColor: "#22A7F0",
                    pointColor: "#22A7F0",
                    pointStrokeColor: "#fff",
                    pointHighlightFill: "#fff",
                    pointHighlightStroke: "#22A7F0",
                    data: result["arr"]
                }
            ]
        };
        myLineChart = new Chart(ctx).Line(data, options);

        //TOP7

        var ctx, data, myBarChart, option_bars;
        Chart.defaults.global.responsive = true;
        ctx = $('#bar-chart').get(0).getContext('2d');
        option_bars = {
            scaleBeginAtZero: true,
            scaleShowGridLines: true,
            scaleGridLineColor: "rgba(0,0,0,.05)",
            scaleGridLineWidth: 1,
            scaleShowHorizontalLines: true,
            scaleShowVerticalLines: false,
            barShowStroke: true,
            barStrokeWidth: 1,
            barValueSpacing: 5,
            barDatasetSpacing: 3,
            legendTemplate: "<ul class=\"<%=name.toLowerCase()%>-legend\"><% for (var i=0; i<datasets.length; i++){%><li><span style=\"background-color:<%=datasets[i].fillColor%>\"></span><%if(datasets[i].label){%><%=datasets[i].label%><%}%></li><%}%></ul>"
        };
        data = {
            labels: arr3,
            datasets: [
                {
                    label: "My First dataset",
                    fillColor: "rgba(26, 188, 156,0.6)",
                    strokeColor: "#1ABC9C",
                    pointColor: "#1ABC9C",
                    pointStrokeColor: "#fff",
                    pointHighlightFill: "#fff",
                    pointHighlightStroke: "#1ABC9C",
                    data: arr
                }, {
                    label: "My Second dataset",
                    fillColor: "rgba(34, 167, 240,0.6)",
                    strokeColor: "#22A7F0",
                    pointColor: "#22A7F0",
                    pointStrokeColor: "#fff",
                    pointHighlightFill: "#fff",
                    pointHighlightStroke: "#22A7F0",
                    data: arr2
                }
            ]
        };
        myBarChart = new Chart(ctx).Bar(data, option_bars);
    });
});

$(document).ready(function () {
    $(".modify").each(function (i, ele) {

    })
});

var currentGid;
function modifyGoods(obj) {
    var radio = $(".gstate");
    $("#create").fadeOut();
    $("#warning").slideDown(500);
    $.getJSON("getgoods", {
        gid: obj.value
    }, function (result) {
        console.log(result);
        currentGid = obj.value;
        $("#gname").val(result["obj"]["Name"]);
        $("#gprice").val(result["obj"]["Price"]);
        $("#gremark").val(result["obj"]["Remark"]);
        if (result["obj"]["Marketable"] === 1) {
            radio[1].click()
        } else {
            radio[0].click()
        }
    })
}

$(function () {
    var modal = $("#warning");
    $("#miniclose").click(function () {
        modal.fadeOut()
    });
    $("#gdelete").click(function () {
        $.getJSON("deletegoods", {
            gid: currentGid
        }, function (result) {
            console.log(result);
            console.log(result["err"]);
            if (result["err"] === null) {
                alert("删除成功!");
                window.location.reload()
            } else {
                modal.fadeOut(500);
            }
        })
    });
    $("#gcancle").click(function () {
        modal.fadeOut(500);
    });
    $("#gsure").click(function () {
        var radio = $(".gstate");
        var state;
        if (radio[0].checked === true) {
            state = 0
        } else {
            state = 1
        }
        var data = {
            id: currentGid,
            name: $("#gname")[0].value,
            price: $("#gprice")[0].value,
            remark: $("#gremark")[0].value,
            marketable: state,
            motion: "update",
        };
        $.getJSON("updategoods", data, function (result) {
            console.log(result);
            if (result["err"] === null && result["row"] === 1) {
                alert("修改成功!");
                window.location.reload()
            } else {
                modal.fadeOut(500);
            }
        })
    })
});

$(function () {
    modal = $("#create");
    $("#creategoods").click(function () {
        $("#warning").fadeOut(500);
        modal.slideDown()
    });

    $("#gcancle2").click(function () {
        modal.fadeOut(500);
    });
    $("#gsure2").click(function () {
        var radio = $(".gstate2");
        var state;
        if (radio[0].checked === true) {
            state = 0
        } else {
            state = 1
        }
        var data = {
            name: $("#gname2")[0].value,
            price: $("#gprice2")[0].value,
            remark: $("#gremark2")[0].value,
            marketable: state,
            motion: "create",
        };
        $.getJSON("updategoods", data, function (result) {
            console.log(result);
            if (result["err"] === null && result["row"] === 1) {
                alert("增加成功!");
                window.location.reload()
            } else {
                modal.fadeOut(500);
            }
        })
    });
    $("#miniclose2").click(function () {
        modal.fadeOut()
    })
});
